#ifndef __HTU21D_I2C_H__
#define __HTU21D_I2C_H__

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "app/framework/include/af.h"

/* 平台相关接口定义
*/
#define I2C_SDA_PIN                   (5U)
#define I2C_SDA_PORT                  (gpioPortA)
#define I2C_SCL_PIN                   (6U)
#define I2C_SCL_PORT                  (gpioPortA)

#define I2C_GPIO_OUT(PORT, PIN)       GPIO_PinModeSet(PORT, PIN, gpioModePushPull, 1)
#define I2C_GPIO_IN(PORT, PIN)        GPIO_PinModeSet(PORT, PIN, gpioModeInput, 1)
#define I2C_GPIO_SET(PORT, PIN)       GPIO_PinOutSet(PORT, PIN)
#define I2C_GPIO_CLEAR(PORT, PIN)     GPIO_PinOutClear(PORT, PIN)
#define I2C_GPIO_READ(PORT, PIN)      GPIO_PinInGet(PORT, PIN)

#define I2C_DELAY_US(T)               USTIMER_Delay(T)
#define I2C_DEBUG_PRINT(...)          emberAfCorePrintln(__VA_ARGS__)
/* end */

#define I2C_SDA_OUT()                 I2C_GPIO_OUT(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SDA_IN()                  I2C_GPIO_IN(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SCL_OUT()                 I2C_GPIO_OUT(I2C_SCL_PORT, I2C_SCL_PIN)
#define I2C_SDA_HIGH()                I2C_GPIO_SET(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SDA_LOW()                 I2C_GPIO_CLEAR(I2C_SDA_PORT, I2C_SDA_PIN)
#define I2C_SCL_HIGH()                I2C_GPIO_SET(I2C_SCL_PORT, I2C_SCL_PIN)
#define I2C_SCL_LOW()                 I2C_GPIO_CLEAR(I2C_SCL_PORT, I2C_SCL_PIN)
#define I2C_SDA_READ()                I2C_GPIO_READ(I2C_SDA_PORT, I2C_SDA_PIN)

/* 寄存器定义 */
#define HTU21D_ADDR                   (0x80)
#define HTU21D_REG_RESET              (0xFE)
#define HTU21D_REG_TEMP               (0xE3)
#define HTU21D_REG_HUMI               (0xE5)
/* end */

void i2c_init();
int htu21d_reset();
int htu21d_read_temperature(int16_t *temperature_mC);
int htu21d_read_humidity(int16_t *humidity_mP);

#endif // __HTU21D_I2C_H__
